
* -------------------------- *
* ESTIMATION OF ELASTICITIES * 
* -------------------------- *

est clear

global exogenous ln_p_inpf_sbjo 
global endogenous ln_q_sbjo
global instrument ln_q_sbq_inst3b
global interaction relational_quartile
global product type_of_product
global fes gr_sjt

sort *
egen gr_rsjt=group(gr_sjt relational_quartile)
egen gr_Rsjt=group(gr_sjt D_sourcing)

* Sample for estimation of elasticities
capture drop SAMPLE_THETA
gen SAMPLE_THETA=(S_pdsivf==1 & n_ud==1 )


* ------------------------------------------------- UNIQUE ELASTICITIES: OLS, IV1, IV2 and FEs  ------------------------------------------------- *


* 1: OLS, one theta 

capture reghdfe ln_WR_o ${exogenous}  ${endogenous} if SAMPLE_THETA==1, abs(${fes}) 
      capture nlcom (theta:(1+_b[ln_p_inpf_sbjo])/(1+_b[ln_q_sbjo])) ///
	  (beta: ((-(1+_b[ln_p_inpf_sbjo])/(1+_b[ln_q_sbjo]))*_b[ln_p_inpf_sbjo])/(_b[ln_p_inpf_sbjo]+1)) ///
	  (thetabeta: (1+_b[ln_p_inpf_sbjo])/(1+_b[ln_q_sbjo])+((-(1+_b[ln_p_inpf_sbjo])/(1+_b[ln_q_sbjo]))*_b[ln_p_inpf_sbjo])/(_b[ln_p_inpf_sbjo]+1)), post

capture gen theta_1_ols=_b[theta]
capture gen beta_1_ols=_b[beta]
capture gen thetabeta_1_ols=_b[thetabeta]
est clear

 	  
* 2: IV, one theta 

capture ivreghdfe ln_WR_o ${exogenous}  (${endogenous}=${instrument}) if SAMPLE_THETA==1, abs(${fes})  
capture nlcom (theta:(1+_b[ln_p_inpf_sbjo])/(1+_b[ln_q_sbjo])) ///
	  (beta: ((-(1+_b[ln_p_inpf_sbjo])/(1+_b[ln_q_sbjo]))*_b[ln_p_inpf_sbjo])/(_b[ln_p_inpf_sbjo]+1)) ///
	  (thetabeta: (1+_b[ln_p_inpf_sbjo])/(1+_b[ln_q_sbjo])+((-(1+_b[ln_p_inpf_sbjo])/(1+_b[ln_q_sbjo]))*_b[ln_p_inpf_sbjo])/(_b[ln_p_inpf_sbjo]+1)), post

capture gen theta_1_iv=_b[theta]
capture gen beta_1_iv=_b[beta]
capture gen thetabeta_1_iv=_b[thetabeta]
est clear
		    
  
* 3. IV, one theta, instrumenting for fabric as well

capture ivreghdfe ln_WR_o   ( ${endogenous} ${exogenous}  = ${instrument} ln_cotton_price_x_rate c.ln_cotton_price_x_rate#i1.numf_orig ) ///
 if SAMPLE_THETA==1, abs(${fes})  	
	     
capture nlcom (theta:(1+_b[ln_p_inpf_sbjo])/(1+_b[ln_q_sbjo])) ///
	  (beta: ((-(1+_b[ln_p_inpf_sbjo])/(1+_b[ln_q_sbjo]))*_b[ln_p_inpf_sbjo])/(_b[ln_p_inpf_sbjo]+1)) ///
	  (thetabeta: (1+_b[ln_p_inpf_sbjo])/(1+_b[ln_q_sbjo])+((-(1+_b[ln_p_inpf_sbjo])/(1+_b[ln_q_sbjo]))*_b[ln_p_inpf_sbjo])/(_b[ln_p_inpf_sbjo]+1)), post

capture gen theta_1_iv2=_b[theta]
capture gen beta_1_iv2=_b[beta]
capture gen thetabeta_1_iv2=_b[thetabeta]
est clear	 
	 
	 
* 4. OLS with buyer fixed effects	 
	
capture ivreghdfe ln_WR_o ${exogenous} ${endogenous} if SAMPLE_THETA==1, abs(${fes} b_id)  
capture nlcom (theta:(1+_b[ln_p_inpf_sbjo])/(1+_b[ln_q_sbjo])) ///
	  (beta: ((-(1+_b[ln_p_inpf_sbjo])/(1+_b[ln_q_sbjo]))*_b[ln_p_inpf_sbjo])/(_b[ln_p_inpf_sbjo]+1)) ///
	  (thetabeta: (1+_b[ln_p_inpf_sbjo])/(1+_b[ln_q_sbjo])+((-(1+_b[ln_p_inpf_sbjo])/(1+_b[ln_q_sbjo]))*_b[ln_p_inpf_sbjo])/(_b[ln_p_inpf_sbjo]+1)), post

capture gen theta_1_bfe=_b[theta]
capture gen beta_1_bfe=_b[beta]
capture gen thetabeta_1_bfe=_b[thetabeta]
est clear
	
	 
* 5. IV with buyer fixed effects

capture ivreghdfe ln_WR_o ${exogenous}  (${endogenous}=${instrument}) if SAMPLE_THETA==1, abs(${fes} b_id)  
capture nlcom (theta:(1+_b[ln_p_inpf_sbjo])/(1+_b[ln_q_sbjo])) ///
	  (beta: ((-(1+_b[ln_p_inpf_sbjo])/(1+_b[ln_q_sbjo]))*_b[ln_p_inpf_sbjo])/(_b[ln_p_inpf_sbjo]+1)) ///
	  (thetabeta: (1+_b[ln_p_inpf_sbjo])/(1+_b[ln_q_sbjo])+((-(1+_b[ln_p_inpf_sbjo])/(1+_b[ln_q_sbjo]))*_b[ln_p_inpf_sbjo])/(_b[ln_p_inpf_sbjo]+1)), post

capture gen theta_1_ivbfe=_b[theta]
capture gen beta_1_ivbfe=_b[beta]
capture gen thetabeta_1_ivbfe=_b[thetabeta]
est clear

* ------------------------------------------------- RELATIONAL DUMMY: OLS AND IV  ------------------------------------------------- *
	 
* 6. OLS with relational dummy	 

capture reghdfe ln_WR_o i.D_sourcing#c.${exogenous} i.D_sourcing#c.${endogenous} if SAMPLE_THETA==1, abs(gr_Rsjt)  
capture nlcom (theta_olsR:(1+_b[1.D_sourcing#c.ln_p_inpf_sbjo])/(1+_b[1.D_sourcing#c.ln_q_sbjo])) ///
	  (beta_olsR: ((-(1+_b[1.D_sourcing#c.ln_p_inpf_sbjo])/(1+_b[1.D_sourcing#c.ln_q_sbjo]))*_b[1.D_sourcing#c.ln_p_inpf_sbjo])/(_b[1.D_sourcing#c.ln_p_inpf_sbjo]+1)) ///
	  (thetabeta_olsR: (1+_b[1.D_sourcing#c.ln_p_inpf_sbjo])/(1+_b[1.D_sourcing#c.ln_q_sbjo])+((-(1+_b[1.D_sourcing#c.ln_p_inpf_sbjo])/(1+_b[1.D_sourcing#c.ln_q_sbjo]))*_b[1.D_sourcing#c.ln_p_inpf_sbjo])/(_b[1.D_sourcing#c.ln_p_inpf_sbjo]+1)) ///	  
	  (theta_olsS:(1+_b[0.D_sourcing#c.ln_p_inpf_sbjo])/(1+_b[0.D_sourcing#c.ln_q_sbjo])) ///
	  (beta_olsS: ((-(1+_b[0.D_sourcing#c.ln_p_inpf_sbjo])/(1+_b[0.D_sourcing#c.ln_q_sbjo]))*_b[0.D_sourcing#c.ln_p_inpf_sbjo])/(_b[0.D_sourcing#c.ln_p_inpf_sbjo]+1)) ///
      (thetabeta_olsS: (1+_b[0.D_sourcing#c.ln_p_inpf_sbjo])/(1+_b[0.D_sourcing#c.ln_q_sbjo])+((-(1+_b[0.D_sourcing#c.ln_p_inpf_sbjo])/(1+_b[0.D_sourcing#c.ln_q_sbjo]))*_b[0.D_sourcing#c.ln_p_inpf_sbjo])/(_b[0.D_sourcing#c.ln_p_inpf_sbjo]+1)), post	  
	  	 
capture gen theta_olsR=_b[theta_olsR]
capture gen beta_olsR=_b[beta_olsR]
capture gen thetabeta_olsR=_b[thetabeta_olsR]
capture gen theta_olsS=_b[theta_olsS]
capture gen beta_olsS=_b[beta_olsS]
capture gen thetabeta_olsS=_b[thetabeta_olsS]
est clear	
	
	 
* 7. IV with relational dummy	 

capture ivreghdfe ln_WR_o i.D_sourcing#c.${exogenous} (i.D_sourcing#c.${endogenous}=i.D_sourcing#c.${instrument}) if SAMPLE_THETA==1, abs(gr_Rsjt)  
capture nlcom (theta_ivR:(1+_b[1.D_sourcing#c.ln_p_inpf_sbjo])/(1+_b[1.D_sourcing#c.ln_q_sbjo])) ///
	  (beta_ivR: ((-(1+_b[1.D_sourcing#c.ln_p_inpf_sbjo])/(1+_b[1.D_sourcing#c.ln_q_sbjo]))*_b[1.D_sourcing#c.ln_p_inpf_sbjo])/(_b[1.D_sourcing#c.ln_p_inpf_sbjo]+1)) ///
	  (thetabeta_ivR: (1+_b[1.D_sourcing#c.ln_p_inpf_sbjo])/(1+_b[1.D_sourcing#c.ln_q_sbjo])+((-(1+_b[1.D_sourcing#c.ln_p_inpf_sbjo])/(1+_b[1.D_sourcing#c.ln_q_sbjo]))*_b[1.D_sourcing#c.ln_p_inpf_sbjo])/(_b[1.D_sourcing#c.ln_p_inpf_sbjo]+1)) ///	  
	  (theta_ivS:(1+_b[0.D_sourcing#c.ln_p_inpf_sbjo])/(1+_b[0.D_sourcing#c.ln_q_sbjo])) ///
	  (beta_ivS: ((-(1+_b[0.D_sourcing#c.ln_p_inpf_sbjo])/(1+_b[0.D_sourcing#c.ln_q_sbjo]))*_b[0.D_sourcing#c.ln_p_inpf_sbjo])/(_b[0.D_sourcing#c.ln_p_inpf_sbjo]+1)) ///
      (thetabeta_ivS: (1+_b[0.D_sourcing#c.ln_p_inpf_sbjo])/(1+_b[0.D_sourcing#c.ln_q_sbjo])+((-(1+_b[0.D_sourcing#c.ln_p_inpf_sbjo])/(1+_b[0.D_sourcing#c.ln_q_sbjo]))*_b[0.D_sourcing#c.ln_p_inpf_sbjo])/(_b[0.D_sourcing#c.ln_p_inpf_sbjo]+1)), post	  
	  	 
capture gen theta_ivR=_b[theta_ivR]
capture gen beta_ivR=_b[beta_ivR]
capture gen thetabeta_ivR=_b[thetabeta_ivR]
capture gen theta_ivS=_b[theta_ivS]
capture gen beta_ivS=_b[beta_ivS]
capture gen thetabeta_ivS=_b[thetabeta_ivS]
est clear	


* ------------------------------------------------- AS IF  ------------------------------------------------- *
	 
* 8. OLS, `as if' exercise 1 (general price of fabric)


capture reghdfe ln_WR_o  ln_av_p_inpf_m ${endogenous}  ///
 if SAMPLE_THETA==1, abs(${fes})  		     
capture nlcom (theta:(1+_b[ln_av_p_inpf_m])/(1+_b[ln_q_sbjo])) ///
	  (beta: ((-(1+_b[ln_av_p_inpf_m])/(1+_b[ln_q_sbjo]))*_b[ln_av_p_inpf_m])/(_b[ln_av_p_inpf_m]+1)) ///
	  (thetabeta: (1+_b[ln_av_p_inpf_m])/(1+_b[ln_q_sbjo])+((-(1+_b[ln_av_p_inpf_m])/(1+_b[ln_q_sbjo]))*_b[ln_av_p_inpf_m])/(_b[ln_av_p_inpf_m]+1)), post	 


capture gen theta_1_ols_asif1=_b[theta]
capture gen beta_1_ols_asif1=_b[beta]
capture gen thetabeta_1_ols_asif1=_b[thetabeta]
est clear

* 9. OLS, `as if' exercise 2 (imputed input-output allocations)


capture reghdfe ln_WR_imputed  ${exogenous} ${endogenous}  ///
 if SAMPLE_THETA==1, abs(${fes})  		     
capture nlcom (theta:(1+_b[ln_p_inpf_sbjo])/(1+_b[ln_q_sbjo])) ///
	  (beta: ((-(1+_b[ln_p_inpf_sbjo])/(1+_b[ln_q_sbjo]))*_b[ln_p_inpf_sbjo])/(_b[ln_p_inpf_sbjo]+1)) ///
	  (thetabeta: (1+_b[ln_p_inpf_sbjo])/(1+_b[ln_q_sbjo])+((-(1+_b[ln_p_inpf_sbjo])/(1+_b[ln_q_sbjo]))*_b[ln_p_inpf_sbjo])/(_b[ln_p_inpf_sbjo]+1)), post	 


capture gen theta_1_ols_asif2=_b[theta]
capture gen beta_1_ols_asif2=_b[beta]
capture gen thetabeta_1_ols_asif2=_b[thetabeta]
est clear

gen dirnum = ${dirnum}

keep dirnum theta* beta* thetabeta*
duplicates drop
order dirnum 

save "${boot}\elasticities_${dirnum}.dta",replace


